<template>
    <exPubTable ref="moneyTable" :cfg.sync="moneycfg" :exData="moneyList" @getDataList="queryMoneyInfo"  :defPageSize="100" :height="300"></exPubTable>
</template>


<script>

export default {
    data() {
        var that =this;
        return {
            bankList: [],
            moneyList:[],
            moneycfg:{
                usePage:true,
                showIndex: true,
                columns: [
                    {prop:"",label:"当前持仓",align:"center",
                        columns:[
                            {prop:"day",label:"日期",width:80},
                            {prop:"zzc",label:"总资产",width:85,formatter:this.bigMoneyFixFormat0,sortable:true},
                            {prop:"preZzc",label:"浮动",width:75,align:"right",getColor(s){return that.getRateColor(s.row.preZzc)}},
                            {prop:"zsz",label:"总市值",width:70,formatter:this.bigMoneyFormat},
                            {prop:"kyzj",label:"可用资金",width:70,formatter:this.bigMoneyFixFormat0},
                            {prop:"prekyzj",label:"可用浮动",width:80,align:"right",getColor(s){return that.getRateColor(s.row.prekyzj)}},
                            {prop:"ccyk",label:"持仓盈亏",width:80,formatter:this.bigMoneyFixFormat0,getColor(s){return that.getRateColor(s.row.ccyk)}},
                            {prop:"totalEarn",label:"实际总盈亏",width:100,sortable:true,formatter: this.bigMoneyFixFormat0,getColor(s){return  that.getRateColor(s.row.totalEarn)},tips:"总资产 - 银证转入资金\n总资产为固化数据,若有资金转入转出,会影响计算"},
                            {prop:"createtime",label:"创建日期",width:140,sortable:true},
                        ]
                    }
                ],
            },
        }
    },

    mounted() {
        this.querySumBankInfo();
    },
    inject:["getQueryInfo"],

    methods: {
        getBankMoney(day){
            var money = 0;
            this.bankList.map(item=>{
                if(item.day<=day){
                    money += item.money;
                }
            })
            return money;
        },
        reQuery(){
            if(this.$refs.moneyTable) this.$refs.moneyTable.reQuery();
        },
        querySumBankInfo(){
            var opername = "getSumBankInfo";
            this.registerListen(this,opername,(res)=>{
                if(res.succ){
                    this.bankList = res.data || [];
                }else{
                    this.$message.error("查询异常:"+res.err)
                }
            });
            
            var param = {type:opername,name:opername,objInfo:{}};
            param.execSql=true;
            param.sql=`select * from bank where status=2`;
            electron.ipcRenderer.send("doSql",param)
        },
        queryMoneyInfo(pInfo){
            var opername = "queryMoneyInfo";
            this.registerListen(this,opername,(res)=>{
                if(res.succ){
                    pInfo.total = res.data.total;
                    this.moneyList = res.data.records;
                    this.moneyList.map((item,idx)=>{
                        item.createtime = this.$dayjs(item.createtime).format("YYYY-MM-DD HH:mm:ss");
                        var preItem = this.moneyList[idx+1];
                        if(preItem){
                            item.preZzc =( item.zzc-preItem.zzc).toFixed(2);
                            item.prekyzj =( item.kyzj-preItem.kyzj).toFixed(2);
                        }

                        item.totalEarn = item.zzc - this.getBankMoney(item.day)
                    });
                }else{
                    this.$message.error("查询异常:"+res.err)
                }
            });
            var arg = this.getQueryInfo();
            delete arg.code;
            
            var param = {type:"commonQry",name:opername,table:"moneyinfo",objInfo:arg};
            param.order = "day desc";
            param.count = true;
            if(pInfo){
                param.pInfo=`${pInfo.page},${pInfo.size}`
            }
            electron.ipcRenderer.send("doSql",param)
        },
    }
    
}
</script>

<style lang="less">

</style>
